<template>
    <div id="sale_records">
        <ListPage>
            <template #filter>
                <div>
                    <el-form ref="form" :model="searchForm" inline label-width="80px">
                        <el-form-item label="">
                            <el-input v-model="searchForm.cn" placeholder="兑换订单编号" clearable
                                style="width: 200px;"></el-input>
                        </el-form-item>
                        <el-form-item label="">
                            <el-select v-model="searchForm.memberId" @change="$forceUpdate()" placeholder="请选择会员账号"
                                clearable style="width: 150px;">
                                <el-option v-for="item in options_memberPhone" :key="item.id" :label="item.name"
                                    :value="item.id" />
                            </el-select>
                        </el-form-item>
                        <el-form-item label="">
                            <el-col :span="11">
                                <el-date-picker type="date" placeholder="选择日期" clearable v-model="searchForm.startTime"
                                    style="width: 150px;"></el-date-picker>
                            </el-col>
                            <el-col class="line flex-c-c" :span="2">-</el-col>
                            <el-col :span="11">
                                <el-date-picker type="date" placeholder="选择日期" clearable v-model="searchForm.endTime"
                                    style="width: 150px;"></el-date-picker>
                            </el-col>
                            <!-- TODO 优化时间选择 -->
                            <!-- <el-date-picker v-model="value2" type="daterange" align="right" unlink-panels range-separator="至"
                    start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions">
                </el-date-picker> -->
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" size="small" @click="submitSearchForm">搜索</el-button>
                            <el-button size="small">重置</el-button>
                        </el-form-item>
                    </el-form>
                </div>
            </template>

            <template #table>
                <el-table :data="tableData" v-loading="loading" style="width: 100%" size="medium">
                    <el-table-column prop="cn" width="200" label="编号">
                    </el-table-column>
                    <el-table-column prop="goodsCoverUrl" label="商品封面">
                        <template v-slot="scope">
                            <img :src="BaseApi + scope.row.goodsCoverUrl" alt="兑换商品图片" height="50px" />
                        </template>
                    </el-table-column>
                    <el-table-column prop="goodsName" label="商品">
                    </el-table-column>
                    <el-table-column prop="integral" label="积分">
                    </el-table-column>
                    <el-table-column width="200" prop="memberPhone" label="会员账号">
                    </el-table-column>
                    <el-table-column prop="updateTime" width="200" label="兑换时间">
                    </el-table-column>
                    <el-table-column prop="updateby" label="操作者">
                    </el-table-column>
                    <el-table-column width="100" fixed="right" label="操作">
                        <template v-slot="scope">
                            <el-button type="danger" size="small" @click="del(scope.row.cn)">
                                删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </template>

            <template #pagination>
                <div>
                    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                        :current-page.sync="searchForm.currentPage" :page-sizes="[20, 50, 100]"
                        :page-size="searchForm.pageSize" layout="total,sizes, prev, pager, next,jumper"
                        :total="searchForm.total">
                    </el-pagination>
                </div>
            </template>
        </ListPage>
    </div>
</template>
<script>
import { popup } from "@/assets/js/common";
import {
    delExchangePointProducts,
    queryOptionsMemberPhone,
    queryPageByQoExchangePointProducts,
} from "@/api/sale_management/exchange_point_products/exchangePointProductsApi";
import ListPage from "../../../component/list-page.vue"

export default {
    components: {
        ListPage
    },
    data() {
        return {
            BaseApi: this.$store.state.BaseApi,
            searchForm: {
                pageSize: 5,
            },
            tableData: [],
            options_memberPhone: [],
            loading: false,
        };
    },
    methods: {
        queryOptionsMemberPhone() {
            queryOptionsMemberPhone().then((res) => {
                res = res.data;
                if (res.code == 200) {
                    this.options_memberPhone = res.data;
                } else {
                    popup(res.msg, "error");
                }
            });
        },
        init() {
            if (this.searchForm.startTime > this.searchForm.endTime) {
                popup("结束时间不能小于开始时间", "warning");
                return;
            }
            this.loading = true;
            queryPageByQoExchangePointProducts(this.searchForm).then((res) => {
                res = res.data;
                if (res.code == 200) {
                    this.tableData = res.data.records;
                    this.searchForm.total = res.data.total;
                    this.searchForm.pageSize = res.data.size;
                    this.searchForm.currentPage = res.data.current;
                } else {
                    popup(res.msg, "error");
                }
            })
            .finally(() => {
                this.loading = false;
            });;
        },
        del(cn) {
            this.$confirm("确定删除这条记录？", "警示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    delExchangePointProducts({ cn: cn }).then((res) => {
                        res = res.data;
                        if (res.code == 200) {
                            popup("操作成功");
                            this.init();
                        } else {
                            popup(res.msg, "error");
                        }
                    });
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消操作",
                    });
                });
        },
        /*分页*/
        handleSizeChange(val) {
            this.searchForm.pageSize = val;
            this.init();
        },
        handleCurrentChange(val) {
            this.searchForm.currentPage = val;
            this.init();
        },
        submitSearchForm() {
            this.init();
        },
    },
    mounted() {
        this.init();
        this.queryOptionsMemberPhone();
    },
};
</script>
<style>
.searchForm {
    height: 134px;
}

.table {
    height: 320px;
}

.pageUtils {
    position: absolute;
    width: 84%;
    margin: 2px auto;
    height: 43px;
}

.searchForm {
    display: inline-block;
    margin: 0 auto;
    height: 51px;
    line-height: 51px;
}

.searchForm .column {
    display: inline-block;
    margin: 0 auto;
    margin-right: 2px;
    line-height: 48px;
    height: 51px;
}
</style>